/*
 * @Author: xmily
 * @Date: 2021-12-20 19:19:36
 * @LastEditors: xmily
 * @LastEditTime: 2023-02-04 23:08:09
 * @FilePath: \glxxapi\routes\wechat\notice.js
 * @Description: 
 * 
 * Copyright (c) 2023 by xmily/mxthink.com.cn, All Rights Reserved. 
 */
const router = require('koa-router')()
const noticeRece = require('../../models/Notice/noticeRece.js');
const notice=require("../../models/Notice/notice.js")
const base = require('../../models/Fjxx');
const sitecfg = require('../../cfg/siteconfig.js')
const commMethod=require("../../cfg/commMethod.js")
var wxuserinfo = null;

router.prefix('/notice')

var term=""
var termlist=[]
router.use(async (ctx, next) => {
  termlist = await commMethod.getTermList();
  term = termlist.sort()[termlist.length - 1];
  await next();
});

//登录用户获取所有通知列表
router.get('/getNoticeByAccount/', async (ctx, next) => {
    let userinfo=ctx.request.token;
    try {
        let res = await noticeRece.getNoticeByAccount({"accountid":userinfo.userid,"fundesc" : null },{"status":1,"noticeid":-1})
        ctx.body = { "error": false, result: res }
    } catch (err) {
        throw new Error("读取通知列表时出错:[" + err + "]");
    }
})

//获取指定ID的通知内容
router.get("/getNoticeById/",async(ctx,next)=>{
    let id=ctx.query.id
    try {
        let res = await notice.GetOneByQuery({"_id":id})
        ctx.body = { "error": false, result: res }
    } catch (err) {
        throw new Error("根据ID读取通知时出错:[" + err + "]");
    }
})

//存储签名
router.post('/saveNoticeSignByAccount/',async (ctx,next)=>{
    let userinfo=ctx.request.token;
    let id=ctx.request.body.id;
    let sign=ctx.request.body.sign;
    try{
        let res=await noticeRece.saveSign(id,sign);
        ctx.body={'error':false,result:res}
    }catch(err){
        throw new Error('保存通知签名时出错：['+err+']');
    }
})

//仅保存签字
router.post('/saveSign/',async (ctx,next)=>{
    let userinfo=ctx.request.token;
    // let id=ctx.request.body.id;
    let sign=ctx.request.body.sign;
    sign.term=term
    try{
        let res=await noticeRece.Save(sign);
        ctx.body={'error':false,result:res}
    }catch(err){
        throw new Error('仅保存签名时出错：['+err+']');
    }
})

//根据指定的查询条件从通知签收库中获取符合条件的数据
router.get('/getNoticeByQuery',async(ctx,next)=>{
    let userinfo=ctx.request.token;
    // let sign=ctx.request.body.sign;
    let noticeid=ctx.query.noticeid;
    try{
        let res=await noticeRece.GetOneByQuery({"noticeid":noticeid,"accountid":userinfo.userid,"term":term})
        ctx.body={'error':false,result:res}
    }catch(err){
        throw new Error('仅保存签名时出错：['+err+']');
    }
})

//删除符合查询条件的记录
router.get("/delNoticeReceByQuery",async(ctx,next)=>{
    let userinfo=ctx.request.token;
    let noticeid=ctx.query.noticeid
    try{
        let res=await noticeRece.delete({"accountid":userinfo.userid,"term":term,"noticeid":noticeid})
        ctx.body={'error':false,result:res}
    }catch(err){
        throw new Error('删除存签名时出错：['+err+']');
    }
})

module.exports = router